<?php

/**
 * A module that extends the functionality of module bootstraps.
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    modules
 * @subpackage modules
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Bootstrap.php 49 2009-05-29 16:27:11Z monzee $
 */
class Modules_Bootstrap extends Zend_Application_Module_Bootstrap
{
    protected $_prefix = 'assets';
    protected $_assetDir = 'public';

    /**
     * Route all requests to this module to the IndexController.
     */
    protected function _initAssetRoute()
    {
        $prefix = trim($this->_prefix, '/');
        $assetDir = trim($this->_assetDir, '/');

        $front = $this->getApplication()->getResource('frontController');
        $router = $front->getRouter();

        $router->addRoute('moduleAsset', new Zend_Controller_Router_Route_Regex(
            $prefix . '/([^/]+)/(.+)',
            array(
                'module' => 'modules',
                'controller' => 'index',
                'action' => 'index'
            ),
            array(
                'target' => 1,
                'asset' => 2
            ),
            $prefix . '/%s/%s'
        ));

        $front->setParam('assetDirectory', $assetDir);
    }

    /**
     * Registers public module asset link generator helper
     */
    protected function _initViewHelpers()
    {
        $app = $this->getApplication();
        if ($app->hasResource('view')) {
            $view = $app->getResource('view');
        } else {
            $renderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
            $renderer->initView();
            $view = $renderer->view;
        }
        $view->addHelperPath(dirname(__FILE__) . '/views/helpers', 'Modules_View_Helper');
    }

    /**
     * Registers module init plugin
     */
    protected function _initModuleDispatchHooks()
    {
        $app = $this->getApplication();
        $front = $app->getResource('frontController');
        $front->registerPlugin(new Modules_Plugin_DispatchHooks($app));
    }

    /**
     * Will only be executed if the request maps to this module.
     */
    public function routeShutdown()
    {
        // disable accessManager if registered
        if (Zend_Controller_Action_HelperBroker::hasHelper('AccessManager')) {
            Zend_Controller_Action_HelperBroker::getStaticHelper('AccessManager')
                                               ->disable();
        }
    }
}
